package pro.softzhang.algo.lc200;

import pro.softzhang.algo.flag.x.XOptimal;

/**
 *
 */
public class LC283_MoveZeroes {
    public static void main(String[] args) {
        int[] arr = {0,1,0,3,12};
        new Solution1().moveZeroes(arr);
    }

    static class Solution {
        public void moveZeroes(int[] nums) {
            int j = 0;
            for(int i = 0; i < nums.length; i++) {
                if(nums[i] != 0) {
                    nums[j++] = nums[i];
                }
            }
            for(int i = j; i < nums.length; i++) {
                nums[i] = 0;
            }
        }
    }

    @XOptimal
    static class Solution1 {
        public void moveZeroes(int[] nums) {
            int j = 0;
            for(int i = 0; i < nums.length; i++) {
                if(nums[i] != 0) {
                    int tmp = nums[i];
                    nums[i] = nums[j];
                    nums[j++] = tmp;
                }
            }
        }
    }
}
